<template>
  <div class="goods-container">
    <!-- 左侧图片区域 -->
    <div class="left">
      <div class="custom-control custom-checkbox">
        <input
          type="checkbox"
          class="custom-control-input"
          :id="id"
          :checked="checked"
          @change="onCheckBoxChange"
        />

        <label class="custom-control-label" :for="id">
          <!-- 商品的缩略图 -->
          <img :src="thumb" alt="商品图片" class="thumb" />
        </label>
      </div>
    </div>
    <!-- 右侧信息区域 -->
    <div class="right">
      <!-- 商品名称 -->
      <div class="top">{{ title }}</div>
      <div class="bottom">
        <!-- 商品价格 -->
        <div class="price">{{ price.toFixed(2) }}</div>
        <!-- 商品数量 -->
        <div class="count">
          <EsCount :num="count" :min="1" @numChange="getNumber"></EsCount>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import EsCount from './EsCount.vue';
export default {
  name: 'EsGoods',
  components: { EsCount },
  props: {
    //   唯一的key值
    id: {
      type: [String, Number],
      required: true,
    },
    //商品缩略图
    thumb: {
      type: String,
      required: true,
    },
    // 商品名称
    title: {
      type: String,
      required: true,
    },
    // 单价
    price: {
      type: Number,
      required: true,
    },
    // 商品数量
    count: {
      type: Number,
      required: true,
    },
    // 勾选状态
    checked: {
      type: Boolean,
      required: true,
    },
  },
  methods: {
    onCheckBoxChange(e) {
      this.$emit('stateChange', {
        id: this.id,
        ischecked: e.target.checked,
      });
    },
    getNumber(val) {
      //   触发自定义事件
      // 向外传递数据对象
      this.$emit('countChange', { id: this.id, num: val });
    },
  },
};
</script>

<style lang="scss" scoped>
.goods-container {
  display: flex;
  padding: 10px;

  + .goods-container {
    border-top: 1px solid #efefef;
  }
  /* 左侧图片的样式 */
  .left {
    margin-right: 10px;
    /* 商品图片 */
    .thumb {
      display: block;
      width: 100px;
      height: 100px;
      background-color: #efefef;
    }
  }
  /* 右侧商品名称、单价、数量的样式 */
  .right {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    flex: 1;
    .top {
      font-weight: bold;
    }
    .bottom {
      display: flex;
      justify-content: space-between;
      align-items: center;
      .price {
        color: red;
        font-weight: bold;
      }
    }
  }
}

.custom-checkbox .custom-control-label::before,
.custom-checkbox .custom-control-label::after {
  top: 3.4rem;
}
</style>
